package com.example.task;

import com.example.bean.RadarTarget;
import com.example.manager.AisManager;
import com.example.manager.RadarManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 * 类描述
 *
 * @author: Fred
 * @email 453086@qq.com
 * @create: 2024-12-20 10:50
 */
@Slf4j
@Component
@EnableScheduling
public class RadarScheduledTask {

    private final RadarManager radarManager = RadarManager.getInstance();
    private final AisManager aisManager = AisManager.getInstance();

    @Scheduled(cron = "*/10 * * * * *")
    public void clearReceivedMessageSet() {
        // 清理2分钟之前的数据
        Map<String, List<RadarTarget>> radarDataHistory = radarManager.getRadarDataHistory();
        for (String key : radarDataHistory.keySet()) {
            LocalDateTime dataTime = LocalDateTime.parse(key, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            if (Duration.between(dataTime, LocalDateTime.now()).getSeconds() > 2 * 60) {
                radarManager.removeRadarDataHistory(key);
            }
//            if (dateTime != null && dateTime.plusMinutes(3).isBefore(LocalDateTime.now())) {
        }
//        System.out.println("打印历史雷达数据：" + JSONUtil.toJsonStr(radarDataHistory));
    }
}
